package com.future.myjavatools.core.zhoupu;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 集合工具类
 *
 * @author zujul
 * @date 2017/8/14
 * @remark
 */
public class CollectionUtil {

    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNotEmpty(Map map) {
        return !isEmpty(map);
    }

    public static <K, V> boolean isEmpty(Multimap<K, V> multimap) {
        return multimap == null || multimap.isEmpty();
    }

    public static <K, V> boolean isNotEmpty(Multimap<K, V> multimap) {
        return !isEmpty(multimap);
    }

    public static boolean isEmpty(Collection collection) {
        return (collection == null || collection.isEmpty());
    }

    public static boolean isNotEmpty(Collection collection) {
        return !isEmpty(collection);
    }

    public static int size(Object object) {
        if (object == null) {
            return 0;
        }
        int total = 0;
        if (object instanceof Map) {
            total = ((Map) object).size();
        } else if (object instanceof Collection) {
            total = ((Collection) object).size();
        } else if (object instanceof Object[]) {
            total = ((Object[]) object).length;
        } else if (object instanceof Iterator) {
            Iterator it = (Iterator) object;
            while (it.hasNext()) {
                total++;
                it.next();
            }
        } else if (object instanceof Enumeration) {
            Enumeration it = (Enumeration) object;
            while (it.hasMoreElements()) {
                total++;
                it.nextElement();
            }
        } else {
            try {
                total = Array.getLength(object);
            } catch (IllegalArgumentException ex) {
                throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
            }
        }
        return total;
    }

    public static <T> Map<Long, T> convertLongMap(Map<String, T> stringKeyMap, String keyPrefix) {
        if (stringKeyMap == null) {
            return null;
        }
        Map<Long, T> longMap = Maps.newHashMapWithExpectedSize(stringKeyMap.size());
        int start = keyPrefix.length();
        for (Map.Entry<String, T> entry : stringKeyMap.entrySet()) {
            long longKey = Long.parseLong(StringUtils.substring(entry.getKey(), start));
            longMap.put(longKey, entry.getValue());
        }
        return longMap;
    }

    public static <T> List<T> conventDownList(Collection<List<T>> collection) {
        if (collection == null) {
            return null;
        }
        List<T> list = Lists.newArrayListWithExpectedSize(collection.size());
        for (List<T> sublist : collection) {
            if (isEmpty(sublist)) {
                continue;
            }
            list.addAll(sublist);
        }
        return list;
    }

    /**
     * 获取List集合的差集
     *
     * @param big   大集合
     * @param small 小集合
     * @return 两个集合的差集
     */
    public static <T> List<T> difference(List<T> big, List<T> small) {
        Set<T> differenceSet = Sets.difference(Sets.newHashSet(big), Sets.newHashSet(small));
        return Lists.newArrayList(differenceSet);
    }

    /**
     * 判断其中一个集合big是否包含集合small里面的任意一个元素
     *
     * @param big
     * @param small
     * @param <T>
     * @return
     */
    public static <T> boolean containsAny(Set<T> big, Set<T> small) {
        return CollectionUtil.isNotEmpty(Sets.intersection(big, small));
    }
}
